<?php

namespace App\Pay;

use App\Jobs\RetailJob;
use App\Identity\Pay;
use App\Models\Order;
use App\Models\User;
use App\Repositories\AmountRepo;
use Illuminate\Support\Facades\DB;

class Wechat implements Pay
{
    public function handel(Order $order,User $user,float $amount)
    {
        DB::beginTransaction();
        try {
            //1.更新支付状态
            $order->pay_time = date('Y-m-d H:i:s');
            $order->pay_money = $amount;
            $order->pay_status = Order::PAYSTATUS;
            $order->save();
            //2.更新用户积分
            $user->intergral += intval($amount);
            $user->save();
            //3.增加积分记录
            AmountRepo::genneral($user,'integral',$amount,1,$order->id);
            DB::commit();
        }catch (\Exception $e) {
            DB::rollBack();
        }

        //2.处理分销
        RetailJob::dispatch($order);
    }
}
